package cloud.maque.admin.controller;

import cloud.maque.admin.domain.vo.MqXzqAreaVo;
import cloud.maque.admin.service.MqXzqAreaService;
import cloud.maque.framework.common.response.ResponseBodyVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 行政区区域
 *
 * @author zhuyf
 * @date 2022/6/28 15:52
 */
@RestController
@RequestMapping("/xzq/area")
@RefreshScope //自动刷新nacos配置
@Api(value = "组件-行政区", tags = "组件-行政区")
public class XzqAreaController {

    @Autowired
    MqXzqAreaService areaService;

    /**
     * 行政区树
     *
     * @return
     * @ignore
     */
    @GetMapping("/areaXzqtree")
    @ApiOperation(value = "获取区域树(无缓存,由运维使用)")
    public ResponseBodyVo<List<MqXzqAreaVo>> AreamXzqTree(@RequestParam(name = "parentId", required = false) String parentId) {

        List<MqXzqAreaVo> result = areaService.xzqAreaTree("", parentId, "", false, false);
        return ResponseBodyVo.Success("成功", result);
    }

    /**
     * 从缓存中读取数据，该接口主要对外和业务层面
     * 两个参数只取其一
     *
     * @return
     */
    @GetMapping("/areaXzqtreeWithCache")
    @ApiOperation(value = "获取区域树")
    public ResponseBodyVo<List<MqXzqAreaVo>> XzqTreeWithCache(@RequestParam(name = "areaCode", required = false) @ApiParam(value = "区域代码", required = true) String areaCode, @RequestParam(name = "areaId", required = false) String areaId) {
        List<MqXzqAreaVo> result = areaService.xzqAreaTree(areaCode, "", areaId, true, true);
        return ResponseBodyVo.Success("成功", result);
    }


    /**
     * 单个行政区信息
     *
     * @return
     * @ignore
     */
    @GetMapping("/areaDetailById/{areaId}")
    public ResponseBodyVo<MqXzqAreaVo> DetailById(@PathVariable("areaId") String xzqId) {
        MqXzqAreaVo result = areaService.DetailById(xzqId);
        return ResponseBodyVo.Success("成功", result);
    }


    /**
     * 保存行政区域
     *
     * @return
     * @ignore
     */
    @PostMapping("/areaSave")
    public ResponseBodyVo<MqXzqAreaVo> Save(@RequestBody MqXzqAreaVo xzq) {
        MqXzqAreaVo result = areaService.Save(xzq);
        return ResponseBodyVo.Success("保存成功", result);
    }


    /**
     * 删除行政区区域
     *
     * @return
     * @ignore
     */
    @PostMapping("/areaDelete/{areaId}")
    public ResponseBodyVo<Boolean> Delete(@PathVariable("areaId") String xzqId) {
        Boolean result = areaService.Delete(xzqId);
        return ResponseBodyVo.Success("删除成功", result);
    }


}
